# Basic GUI - A basic GUI for pygame.
# Copyright(c) 2012 Evan Kline
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
#to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
#and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
#IN THE SOFTWARE.

import pygame.font, pygame.draw
import eztext
from pygame import Rect

class BoolButton():
	def __init__(self, pos, wh=(20,20), title='Button', toggletitle=None, initstate=0, colors=[(47,168,203), (0,131,182), (10,10,10)], font=None):
		self.rect = Rect(pos, wh)
		self.state = initstate
		self.colors = colors
		self.font = font
		self.title = title
		self.toggletitle = toggletitle
		self.textsurface = [None, None]
		if font != None:
			self.textsurface[0] = font.render(self.title, 1, colors[2])
			if toggletitle != None:
				self.textsurface[1] = font.render(self.toggletitle, 1, colors[2])
			else:
				self.textsurface[1] = self.textsurface[0]
		
	def testclick(self, xy):
		if self.rect.collidepoint(xy):
			self.toggle()
			return True
		else:
			return False
	
	def toggle(self):
		if self.state:
			self.state = False
		else:
			self.state = True
	
	def draw(self, screen):
		if self.font != None:
			screen.blit(self.textsurface[self.state], (self.rect[0], self.rect[1]-self.textsurface[self.state].get_size()[1]) )
		pygame.draw.rect(screen, self.colors[self.state], self.rect)

class ActionButton():
	def __init__(self, pos, action, wh=(20,20), title='Button', colors=[(47,168,203), (10,10,10)], font=None):
		self.rect = Rect(pos, wh)
		self.colors = colors
		self.font = font
		self.title = title
		self.action = action
		if font != None:
			self.textsurface = font.render(self.title, 1, colors[1])
		
	def testclick(self, xy):
		if self.rect.collidepoint(xy):
			self.action()
		else:
			return False
	
	def draw(self, screen):
		if self.font != None:
			screen.blit(self.textsurface, (self.rect[0], self.rect[1]-self.textsurface.get_size()[1]) )
		pygame.draw.rect(screen, self.colors[0], self.rect)

class MultipleClickButton():
	def __init__(self, pos, action, clicks=2, wh=(20,20), titles=['Button', 'Really Button?'], colors=[(174,68,68), (200,68,68), (10,10,10)], font=None):
		self.rect = Rect(pos, wh)
		self.colors = colors
		self.font = font
		self.titles = titles
		self.action = action
		if font != None:
			self.textsurfaces = []
			for title in titles:
				self.textsurfaces.append(font.render(title, 1, colors[-1]))
		self.clicksreq = clicks
		self.clicks = 0
	
	def testclick(self, xy):
		if self.rect.collidepoint(xy):
			self.clicks += 1
			if self.clicks == self.clicksreq:
				self.action()
				self.clicks = 0
			return True
		else:
			self.clicks = 0
			return False
	
	def draw(self, screen):
		if self.font != None:
			screen.blit(self.textsurfaces[self.clicks], (self.rect[0], self.rect[1]-self.textsurfaces[self.clicks].get_size()[1]) )
			
		pygame.draw.rect(screen, self.colors[self.clicks], self.rect)

class OptionSet():
	def __init__(self, colors, wh=(20,20), font=None):
		self.colors = colors
		self.Buttons = []
		self.btnwh = wh
		if font == None:
			self.font = pygame.font.Font(None, 11)
		else:
			self.font = font
		
	def testbuttons(self, xy):
		for bt in self.Buttons:
			if bt.testclick(xy):
				self.buttonswitch(bt)
				break
				
	def buttonswitch(self, button):
		for bt in self.Buttons:
			if bt is not button:
				bt.state = False
		
	def draw(self, screen):
		for bt in self.Buttons:
			bt.draw(screen)
	
	def addButton(self, xy, title='Button', initial=0, colors=None):
		if colors == None:
			colors = self.colors
		btn = BoolButton(xy, self.btnwh, colors = colors, title = title, font = self.font, initstate = initial)
		self.Buttons.append( btn )
		
		if initial == 1:
			self.buttonswitch(btn)
			
		return btn

class TextBox():
	def __init__(self, xy, textfont, maxlen=13, title='TextBox', colors=[(159,212,255),(180,235,255),(10,10,10),(0,0,0)], font=None, unfocusOnClick=True):
		self.pos = xy
		self.title = title
		self.colors = colors
		self.font = font
		if font != None:
			self.titlesurface = font.render(self.title, 1, colors[2])
			
		self.box = eztext.Input(maxlength=maxlen, color=colors[3], font=textfont, x=xy[0], y=xy[1])
		self.state = 0
		self.unfocusOnClick = unfocusOnClick
		self.width, self.height = self.box.font.size("*"*maxlen)
	
	def draw(self, screen):
		pygame.draw.rect(screen, self.colors[self.state], (self.pos[0],self.pos[1], self.width, self.height))
		self.box.draw(screen)
		if self.font != None:
			screen.blit(self.titlesurface, (self.pos[0], self.pos[1]-self.titlesurface.get_size()[1]) )
	
	def testclick(self, xy):
		rect = Rect(self.pos, (self.width, self.height))
		if rect.collidepoint(xy):
			self.state = 1
			return True
		else:
			if self.unfocusOnClick:
				self.state = 0
			return False
	
	def update(self, events):
		self.box.update(events)

class ActionTextBox(TextBox):
	def __init__(self, xy, textfont, action, maxlen=13, title='TextBox', colors=[(120,90,80),(159,212,255),(180,235,255),(10,10,10),(0,0,0)], font=None, unfocusOnClick=True):
		TextBox.__init__(self, xy, textfont, maxlen=maxlen, title=title, colors=colors[1:], font=font, unfocusOnClick=unfocusOnClick)
		
		btxy = (self.pos[0]+self.width, self.pos[1])
		self.button = ActionButton(btxy, self.onAction, wh=(20,self.height), colors=[colors[0], colors[3]])
		
		self.action = action
	
	def draw(self, screen):
		TextBox.draw(self, screen)
		self.button.draw(screen)
	
	def testclick(self, xy):
		self.button.testclick(xy)
		TextBox.testclick(self, xy)
	
	def onAction(self):
		self.action(self.box.value)

class StatusTextLine():
	def __init__(self, xy, font, maxlen=50, colors=[(0,0,0),(40,254,20)], text=''):
		self.pos = xy
		self.font = font
		self.maxlen = maxlen
		self.colors = colors
		self.text = text
		self.width, self.height = self.font.size("*"*maxlen)
	
	def draw(self, screen):
		textsurface = self.font.render(self.text[:self.maxlen], 1, self.colors[1])
		pygame.draw.rect(screen, self.colors[0], (self.pos[0],self.pos[1], self.width+2, self.height))
		screen.blit(textsurface, (self.pos[0]+2, self.pos[1]))

#class GuiContainer():
#	def __init__(self, size):